package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Dept;
import com.hopu.pojo.Emp;
import com.hopu.service.EmpService;
import com.hopu.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("emp")
@RestController
public class EmpController {
    @Resource
    private EmpService empServiceImpl;

    /**
     * 登录
     * @param ename
     * @param epsw
     * @return
     */
    @PostMapping("login")
    private ResultUtils login(@RequestParam String ename, @RequestParam String epsw, HttpServletRequest request){
//        System.out.println(ename+"-----"+epsw);
        Emp emp = empServiceImpl.login(ename, epsw);
        if (!ObjectUtils.isEmpty(emp)){
            request.getSession().setAttribute("empLog",emp);
            return new ResultUtils(200,"SUCCESS",emp);
        }
        return new ResultUtils(400,"Error",null);
    }

    /**
     * 根据ename查找员工是否存在
     * @param ename
     * @return
     */
    @PostMapping("findByEname")
    private ResultUtils findByEname(@RequestParam("ename")String ename){
//        System.out.println(ename);
        Emp emp = empServiceImpl.findByEname(ename);
//        System.out.println(emp);
        if (!ObjectUtils.isEmpty(emp)){
            return new ResultUtils(200,"SUCCESS",emp);
        }
        return new ResultUtils(400,"Error",null);
    }

    /**
     * 查询所有员工根据 分页
     * @param pn
     * @param eflag
     * @return
     */
    @PostMapping("findAllEmp")
    private ResultUtils findAllDept(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                    @RequestParam(value = "eflag",defaultValue = "0") int eflag){
        PageHelper.startPage(pn,14);
        List<Emp> list = empServiceImpl.findAllEmp(eflag);
        PageInfo page = new PageInfo(list,5);
        return  new ResultUtils(200,"SUCCESS",page);
    }

    /**
     * 添加 员工
     * @param jid
     * @param pid
     * @param ename
     * @param epsw
     * @param erealname
     * @param etel
     * @param eaddress
     * @param eremark
     * @param eflag
     * @return
     */
    @PostMapping("addEmp")
    private ResultUtils addEmp(@RequestParam(value = "jid",required = true)int jid,
                               @RequestParam(value = "pid",required = true)int pid,
                               @RequestParam(value = "ename",required = true)String ename,
                               @RequestParam(value = "epsw",required = true)String epsw,
                               @RequestParam(value = "erealname",required = true)String erealname,
                               @RequestParam(value = "etel",required = true)String etel,
                               @RequestParam(value = "eaddress",required = true)String eaddress,
                               @RequestParam(value = "eremark",required = true)String eremark,
                               @RequestParam(value = "eflag",required = true,defaultValue = "0")int eflag){
        int num = empServiceImpl.addEmp(jid,pid,ename,epsw,erealname,etel,eaddress,eremark,eflag);
        System.out.println(num);
        if (num==0){
            return new ResultUtils(400,"ERROR",null);
        }
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 删除员工
     * @param eid
     * @return
     */
    @PostMapping("delEmp")
    private ResultUtils delEmp(@RequestParam(value = "eid",required = true)int eid){
        int num = empServiceImpl.delEmp(eid);
        return new ResultUtils(200,"SUCCESS",1);
    }
    /**
     * 恢复员工
     * @param eid
     * @return
     */
    @PostMapping("huiFuEmp")
    private ResultUtils huiFuEmp(@RequestParam(value = "eid",required = true) int eid){
        System.out.println(eid);
        int num = empServiceImpl.huiFuEmp(eid);
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 修改员工
     * @param eid
     * @return
     */
    @PostMapping("uppEmp")
    private ResultUtils uppEmp(@RequestParam(value = "eid",required = true) int eid,
                                   @RequestParam(value = "jid",required= true) int jid,
                                   @RequestParam(value = "pid",required= true) int pid,
                                   @RequestParam(value = "ename",required = true) String ename,
                                   @RequestParam(value = "epsw",required = true) String epsw,
                                   @RequestParam(value = "erealname",required = true) String erealname,
                                   @RequestParam(value = "etel",required = true) String etel,
                                   @RequestParam(value = "eaddress",required = true) String eaddress,
                                   @RequestParam(value = "eremark",required = true) String eremark,
                                   @RequestParam(value = "eflag",defaultValue = "0") int eflag){
        int num = empServiceImpl.uppEmp(eid,jid,pid,ename,epsw,erealname,etel,eaddress,eremark,eflag);
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 根据id获取用户信息
     * @param eid
     * @return
     */
    @PostMapping("findEmpById")
    private ResultUtils findEmpById(@RequestParam(value = "eid",required = true)String eid){
        Emp emp = empServiceImpl.findEmpById(eid);
        System.out.println(emp);
        if (!ObjectUtils.isEmpty(emp)){
            return new ResultUtils(200,"SUCCESS",emp);
        }
        return new ResultUtils(400,"Error",null);
    }
    @PostMapping("findEmpByss")
    private ResultUtils findEmpByss(HttpSession session){
        Emp empLog = (Emp) session.getAttribute("empLog");
        Emp emp = empServiceImpl.findEmpById(String.valueOf(empLog.getEid()));
        System.out.println(emp);
        if (!ObjectUtils.isEmpty(emp)){
            return new ResultUtils(200,"SUCCESS",emp.getEpsw());
        }
        return new ResultUtils(400,"Error",null);
    }
    @PostMapping("uppPwd")
    private ResultUtils uppPwd(HttpSession session,
                               @RequestParam(value = "epsw",required = true) String epsw)
                              {
      Emp empLog = (Emp) session.getAttribute("empLog");
        int num = empServiceImpl.uppPwd(empLog.getEid(),epsw);
        return new ResultUtils(200,"SUCCESS",1);
    }
}
